package scuba.DiveBuddy.Fragments.NewLogEntry;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import scuba.DiveBuddy.Class.LogEntries;
import scuba.DiveBuddy.Core.Toolbelt;
import scuba.DiveBuddy.Core.Tuple;
import scuba.DiveBuddy.Enums.*;
import scuba.DiveBuddy.R;

/**
 * @author Alex Anderson
 * @version 1.0
 * @since {26/02/14}
 */
public class LogDataFragment extends Fragment
    implements Spinner.OnItemSelectedListener,
    EditText.OnFocusChangeListener {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_editor_data, container, false);

        Spinner s = (Spinner)v.findViewById(R.id.spinWaterType);
        s.setAdapter(new ArrayAdapter<WaterTypeEnum>(getActivity(), R.layout.component_spinneritem, WaterTypeEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinAltitudeUnits);
        s.setAdapter(new ArrayAdapter<DistanceEnum>(getActivity(), R.layout.component_spinneritem, DistanceEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinTempUnits);
        s.setAdapter(new ArrayAdapter<TemperatureUnitsEnum>(getActivity(), R.layout.component_spinneritem, TemperatureUnitsEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinWeightUnits);
        s.setAdapter(new ArrayAdapter<WeightUnitsEnum>(getActivity(), R.layout.component_spinneritem, WeightUnitsEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinVisUnits);
        s.setAdapter(new ArrayAdapter<DistanceEnum>(getActivity(), R.layout.component_spinneritem, DistanceEnum.values()));
        s.setOnItemSelectedListener(this);

        s = (Spinner)v.findViewById(R.id.spinExposureType);
        s.setAdapter(new ArrayAdapter<ExposureTypeEnum>(getActivity(), R.layout.component_spinneritem, ExposureTypeEnum.values()));
        s.setOnItemSelectedListener(this);

        EditText e = (EditText)v.findViewById(R.id.tempSurfaceSelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.tempDepthSelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.altitudeSelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.weightSelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.visibilitySelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.exposureSelect);
        e.setOnFocusChangeListener(this);

        e = (EditText)v.findViewById(R.id.observations);
        e.setOnFocusChangeListener(this);

        return v;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

        if(view == null)
            return;

        int compID = adapterView.getId();
        switch(compID) {
            case R.id.spinWaterType:
                Log.v("INTERFACE", "[DEBUG] waterType=" + WaterTypeEnum.values()[i]);
                LogEntries.getCurrent().setWaterType(WaterTypeEnum.values()[i]);
                break;

            case R.id.spinAltitudeUnits:
                Log.v("INTERFACE", "[DEBUG] altUnits=" + DistanceEnum.values()[i]);
                LogEntries.getCurrent().setAltitudeUnits(DistanceEnum.values()[i]);
                break;

            case R.id.spinTempUnits:
                Log.v("INTERFACE", "[DEBUG] tempUnits=" + TemperatureUnitsEnum.values()[i]);
                LogEntries.getCurrent().setTemperatureUnits(TemperatureUnitsEnum.values()[i]);
                break;

            case R.id.spinWeightUnits:
                Log.v("INTERFACE", "[DEBUG] weightUnits=" + WeightUnitsEnum.values()[i]);
                LogEntries.getCurrent().setWeightUnits(WeightUnitsEnum.values()[i]);
                break;

            case R.id.spinVisUnits:
                Log.v("INTERFACE", "[DEBUG] VisUnits=" + DistanceEnum.values()[i]);
                LogEntries.getCurrent().setVisibilityUnits(DistanceEnum.values()[i]);
                break;

            case R.id.spinExposureType:
                Log.v("INTERFACE", "[DEBUG] exposureType=" + ExposureTypeEnum.values()[i]);
                LogEntries.getCurrent().setExposureSuit(ExposureTypeEnum.values()[i]);
                break;

        }

    }
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        return;
    }

    @Override
    public void onFocusChange(View view, boolean b) {
        if(b) return; //focused

        switch(view.getId()) {
            case R.id.tempSurfaceSelect:
                Tuple<Boolean, Float> result = Toolbelt.Toolkit.getFloatFromString(((EditText) view).getText().toString(), 0);
                if(!result.x)
                    break;

                Log.v("INTERFACE", "User has Closed tempSurfaceSelect with depth = " + result.y);
                LogEntries.getCurrent().setSurfaceTemperature(result.y);
                break;

            case R.id.tempDepthSelect:
                result = Toolbelt.Toolkit.getFloatFromString(((EditText) view).getText().toString(), 0);
                if(!result.x)
                    break;

                Log.v("INTERFACE", "User has Closed tempDepthSelect with depth = " + result.y);
                LogEntries.getCurrent().setDepthTemperature(result.y);
                break;

            case R.id.altitudeSelect:
                result = Toolbelt.Toolkit.getFloatFromString(((EditText) view).getText().toString(), 0);
                if(!result.x)
                    break;

                Log.v("INTERFACE", "User has Closed altitudeSelect with depth = " + result.y);
                LogEntries.getCurrent().setAltitude(result.y);
                break;

            case R.id.weightSelect:
                result = Toolbelt.Toolkit.getFloatFromString(((EditText) view).getText().toString(), 0);
                if(!result.x)
                    break;

                Log.v("INTERFACE", "User has Closed weightSelect with depth = " + result.y);
                LogEntries.getCurrent().setWeight(result.y);
                break;

            case R.id.visibilitySelect:
                result = Toolbelt.Toolkit.getFloatFromString(((EditText) view).getText().toString(), 0);
                if(!result.x)
                    break;

                Log.v("INTERFACE", "User has Closed visibilitySelect with depth = " + result.y);
                LogEntries.getCurrent().setVisibility(result.y);
                break;

            case R.id.exposureSelect:
                result = Toolbelt.Toolkit.getFloatFromString(((EditText) view).getText().toString(), 0);
                if(!result.x)
                    break;

                Log.v("INTERFACE", "User has Closed exposureSelect with depth = " + result.y);
                LogEntries.getCurrent().setExposureProtectionMM(result.y);
                break;

            case R.id.observations:
                String resultString = ((EditText) view).getText().toString();
                if(resultString.isEmpty())
                    break; //nothing enntered dont bother setting, wastes our time

                Log.v("INTERFACE", "User has Closed observations with depth = " + resultString);
                LogEntries.getCurrent().setObservations(resultString);
                break;
        }
    }
}
